const resolve = require('path').resolve;
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const url = require('url');
const publicPath = '';

module.exports = (options = {}) => ({
  entry: {
    // vendor: './src/js/vendor',
    index: './src/js/main.js',
    'babel-polyfill': 'babel-polyfill'
  },
  output: {
    path: resolve(__dirname, 'dist'),
    filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
    chunkFilename: '[id].js?[chunkhash]',
    publicPath: options.dev ? '/assets/' : publicPath
  },
  module: {
    loaders:[
      {
        test: /\.js$/,  //匹配希望处理文件的路径
        loader: ['babel'], 
        include: [require('path').join(publicPath, 'src')],
        query:{
          presets: ["es2015", "stage-0"],
          plugins: [
            "transform-es3-property-literals",
            "transform-es3-member-expression-literals"
          ]
        }
      },
    ],
    rules: [
      {
        test: /\.vue$/,
        use: ['vue-loader']
      },
      {
        test: /\.js$/,
        use: ['babel-loader'],
        exclude: /node_modules/   // 匹配不希望处理文件的路径
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 10000
          }
        }]
      }
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'manifest']
    }),
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    }),
    new CopyWebpackPlugin([
          {
            from: 'exceltemplate/',
            to:'exceltemplate/'
          }
      ]),
    // 设置语言
    new webpack.NormalModuleReplacementPlugin(/element-ui[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]zh-CN/, 'element-ui/lib/locale/lang/en')
  ],
  resolve: {
    alias: {
      '~': resolve(__dirname, 'src')
    }
  },
  devServer: {     // 反向代理  取代了nginx
    host: 'localhost',     //  http://127.0.0.1:8010/
    port: 8010,       //自己指定的端口
    //disableHostCheck: true,     
      proxy: {
        // '/rest-api/real-pscm-iwm/': {    //测试api
        //     target: 'http://10.103.195.140:18089',     //测试api对应的url  吴健
        //     // changeOrigin: true,
        //     pathRewrite: {
        //         '^/rest-api/real-pscm-iwm': ''
        //     }
        // },
        // '/rest-api/pscm-metadata/': {    //测试api
        //   target: 'http://10.103.195.140:18082',     //测试api对应的url --直连吴建
        //   // changeOrigin: true,
        //   pathRewrite: {
        //     '^/rest-api/pscm-metadata': ''
        //   }
        // },

        //  '/rest-api/real-pscm-iwm/': {    //测试api
        //     target: 'http://10.103.122.136:18087',     //测试api对应的url  陈安志
        //     // changeOrigin: true,
        //     pathRewrite: {
        //       '^/rest-api/real-pscm-iwm': ''
        //     }
        //   },
          // '/rest-api/pscm-procurement/': {    //测试api
          //   target: 'http://10.103.187.17:18083',     //测试api对应的url  田成方
          //   // changeOrigin: true,
          //   pathRewrite: {
          //     '^/rest-api/pscm-procurement': ''
          //   }
          // },
          // '/rest-api/pscm-metadata/': {    //测试api
          //   target: 'http://10.103.195.140:18082',     //测试api对应的url --直连吴健--条码
          //   // changeOrigin: true,
          //   pathRewrite: {
          //     '^/rest-api/pscm-metadata': ''
          //   }
          // },
         /* '/rest-api/pscm-metadata/': {    //测试api
            target: 'http://10.103.122.136:18082',     //测试api对应的url --直连陈安志--工程替换组导入、导出
            // changeOrigin: true,
            pathRewrite: {
              '^/rest-api/pscm-metadata': ''
            }
          },*/
          '/rest-api/': {    //测试api
            target: 'http://10.103.237.102:9993',
            // changeOrigin: true,
            pathRewrite: {
                '^/rest-api': ''
            }
          },
          '/local-api/': {    //测试api
              target: 'http://127.0.0.1:18082',     //测试api对应的url
              // changeOrigin: true,
              pathRewrite: {
                  '^/local-api': ''
              }
          }

      },

    historyApiFallback: {
      index: url.parse(options.dev ? '/assets/' : publicPath).pathname
    }
  },
  // devtool: options.dev ? '#eval-source-map' : '#source-map'
  devtool: '#cheap-module-eval-source-map'    //原始代码（只有行内） 同样道理，但是更高的质量和更低的性能
  //eval-source-map 这个就是把 eval 的 sourceURL 换成了完整 souremap 信息的 DataUrl
  //source-map 这是最原始的 source-map 实现方式，其实现是打包代码同时创建一个新的 sourcemap 文件， 并在打包文件的末尾添加 
  //# sourceURL 注释行告诉 JS 引擎文件在哪儿
});
